<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改' " :close-on-click-modal='false'
		:close-on-press-escape='false' :fullscreen='true'>
		<el-form :inline="true" :model="dataForm" :rules="dataRule" ref="dataFormRef" label-width="120px">
			<el-form-item label="入库单号" prop="receiptkey">
				<el-input v-model="dataForm.receiptkey" placeholder="入库单号" disabled></el-input>
			</el-form-item>
			<el-form-item v-if="!!dataForm.id" label="明细行号" prop="line">
				<el-input v-model="dataForm.line" placeholder="明细行号" disabled></el-input>
			</el-form-item>
			<el-form-item label="物料编码" prop="sku">
				<el-input v-model="dataForm.sku" placeholder="物料编码" @change="getSkuFn"></el-input>
			</el-form-item>

			<el-form-item label="物料名称" prop="descr">
				<el-input v-model="dataForm.descr" placeholder="物料名称" @click="openSkuDialogHandle()" readonly>
				</el-input>
			</el-form-item>
			
			<el-form-item label="计量编码/工装号" prop="factorycdoe">
				<el-input v-model="dataForm.factorycdoe" placeholder="计量编码/工装号" @click="openSkuDialogHandle()" readonly>
				</el-input>
			</el-form-item>
					
			<el-form-item label="单位" prop="unit">
				<el-input v-model="dataForm.unit" placeholder="单位" @click="openSkuDialogHandle()" readonly></el-input>
			</el-form-item>
			<el-form-item label="包装" prop="pack">
				<el-input v-model="dataForm.pack" placeholder="包装" @click="openSkuDialogHandle()" readonly></el-input>
			</el-form-item>
			<el-form-item label="状态" prop="status">
				<el-select v-model="dataForm.status " placeholder="请选择" disabled>
					<el-option v-for="item in receiptStatus" :key="item.list" :label="item.listname"
						:value="reString(item.list)">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="预计数量" prop="qty">
				<el-input v-model="dataForm.qty" placeholder="预计数量" onkeyup="value=value.replace(/[^0-9.]/g,'')">
				</el-input>
			</el-form-item>
			<el-form-item label="实际数量" prop="actqty">
				<el-input v-model="dataForm.actqty" placeholder="实际数量" onkeyup="value=value.replace(/[^0-9.]/g,'')">
				</el-input>
			</el-form-item>
			<el-form-item label="目的库位" prop="toloc">
				<el-input v-model="dataForm.toloc" placeholder="目的库位"></el-input>
			</el-form-item>
			<el-form-item label="目的托盘" prop="totray">
				<el-input v-model="dataForm.totray" placeholder="目的托盘"></el-input>
			</el-form-item>
			<el-form-item label="目的容器" prop="tobox">
				<!-- <el-input v-model="dataForm.tobox" placeholder="目的容器"></el-input> -->
				<el-select v-model="dataForm.tobox" placeholder="请选择">
					<el-option v-for="item in boxs" :key="item.list" :label="item.listname" :value="item.list">
					</el-option>
				</el-select>
			</el-form-item>
			<!-- <el-form-item label="批号" prop="lot">
				<el-input v-model="dataForm.lot" placeholder="批号"></el-input>
			</el-form-item> -->
			<el-form-item label="生产日期" prop="lot1">
				<el-date-picker v-model="dataForm.lot1" type="date" placeholder="生产日期" format="YYYY-MM-DD">
				</el-date-picker>
				<!-- <el-input v-model="dataForm.lot1" placeholder="生产日期"></el-input> -->
			</el-form-item>
			<el-form-item label="有效日期" prop="lot2">
				<el-date-picker v-model="dataForm.lot2" type="date" placeholder="有效日期" format="YYYY-MM-DD">
				</el-date-picker>
				<!-- <el-input v-model="dataForm.lot2" placeholder="有效日期"></el-input> -->
			</el-form-item>
			<el-form-item label="是否加长" prop="lot3">
				<!-- <el-input v-model="dataForm.lot3" placeholder="是否加长"></el-input> -->
				<el-select v-model="dataForm.lot3">
					<el-option value="" label="--"></el-option>
					<el-option v-for="item in yesornos" :key="item.list" :label="item.listname" :value="item.list">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="价格" prop="lot4">
				<el-input v-model="dataForm.lot4" placeholder="价格"></el-input>
			</el-form-item>
			<el-form-item label="部门/供应商" prop="lot5">
				<el-input v-model="dataForm.lot5" placeholder="部门/供应商"></el-input>
			</el-form-item>
			<el-form-item label="计划单号/工序号" prop="lot6">
				<el-input v-model="dataForm.lot6" placeholder="计划单号/工序号" @change="getLot6Fn"></el-input>
			</el-form-item>
			<el-form-item label="主制车间" prop="lot7">
				<el-input v-model="dataForm.lot7" placeholder="主制车间"></el-input>
			</el-form-item>
			<el-form-item label="任务编码" prop="lot8">
				<el-input v-model="dataForm.lot8" placeholder="任务编码"></el-input>
			</el-form-item>
			<el-form-item label="新旧" prop="lot9">
				<!-- <el-input v-model="dataForm.lot9" placeholder="新旧"></el-input> -->
				<el-select v-model="dataForm.lot9">
					<el-option value="" label="--"></el-option>
					<el-option v-for="item in newandolds" :key="item.list" :label="item.listname" :value="item.list">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="作废" prop="lot10">
				<!-- <el-input v-model="dataForm.lot10" placeholder="作废"></el-input> -->
				<el-select v-model="dataForm.lot10">
					<el-option value="" label="--"></el-option>
					<el-option v-for="item in yesornos" :key="item.list" :label="item.listname" :value="item.list">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="炉批号" prop="lot11">
				<el-input v-model="dataForm.lot11" placeholder="炉批号"></el-input>
			</el-form-item>
			<el-form-item label="材料复验号" prop="lot12">
				<el-input v-model="dataForm.lot12" placeholder="材料复验号"></el-input>
			</el-form-item>
			<el-form-item label="牌号" prop="lot13">
				<el-input v-model="dataForm.lot13" placeholder="牌号"></el-input>
			</el-form-item>
			<el-form-item label="属性14" prop="lot14">
				<el-input v-model="dataForm.lot14" placeholder="属性14"></el-input>
			</el-form-item>
			<el-form-item label="属性15" prop="lot15">
				<el-input v-model="dataForm.lot15" placeholder="属性15"></el-input>
			</el-form-item>
			<el-form-item label="备注" prop="remark">
				<el-input v-model="dataForm.remark" placeholder="备注"></el-input>
			</el-form-item>
			<el-form-item label="来源单号" prop="sourcekey">
				<el-input v-model="dataForm.sourcekey" placeholder="来源单号"></el-input>
			</el-form-item>
			<el-form-item label="来源单类型" prop="sourcetype">
				<el-input v-model="dataForm.sourcetype" placeholder="来源单类型"></el-input>
			</el-form-item>
			<el-form-item label="来源单时间" prop="sourcedate">
				<el-date-picker v-model="dataForm.sourcedate" type="date" placeholder="来源单时间" format="YYYY-MM-DD">
				</el-date-picker>
				<!--<el-input v-model="dataForm.sourcedate" placeholder="来源单时间"></el-input>-->
			</el-form-item>
			<el-form-item label="关联单据" prop="relatedorderkey">
				<el-input v-model="dataForm.relatedorderkey" placeholder="关联单据"></el-input>
			</el-form-item>
			<el-form-item label="关联单据行" prop="relatedorderline">
				<el-input v-model="dataForm.relatedorderline" placeholder="关联单据行"></el-input>
			</el-form-item>
			<el-form-item label="回传状态" prop="backstatus">
				<el-input v-model="dataForm.backstatus" placeholder="回传状态"></el-input>
			</el-form-item>
			<el-form-item label="回传时间" prop="backdate">
				<el-input v-model="dataForm.backdate" placeholder="回传时间"></el-input>
			</el-form-item>
			<el-form-item label="回传结果" prop="backresult">
				<el-input v-model="dataForm.backresult" placeholder="回传结果"></el-input>
			</el-form-item>
			<el-form-item label="回传码" prop="backcode">
				<el-input v-model="dataForm.backcode" placeholder="回传码"></el-input>
			</el-form-item>
			<!-- <el-form-item label="仓库" prop="warehouse">
				<el-input v-model="dataForm.warehouse" placeholder="仓库"></el-input>
			</el-form-item> -->
		</el-form>
		<div style="margin-top: 40px;font-size: 18px" v-show="isuniqueVisible">
			<span>唯一码管理:</span>
		</div>
		<div style="margin-top: 15px">
			<el-form :inline="true" v-show="isuniqueVisible">
				<el-form-item label="物料编码" prop="sku">
					<el-input v-bind:model-value="dataForm.sku" placeholder="物料编码" disabled></el-input>
				</el-form-item>
				<el-form-item label="物料名称" prop="descr">
					<el-input v-bind:model-value="dataForm.descr" placeholder="物料名称" disabled></el-input>
				</el-form-item>
				<el-form-item label="唯一码" prop="uniqueCode">
					<el-input v-model="message" placeholder="唯一码" @change="putMessage"></el-input>
				</el-form-item>
				<el-form-item label="已输入的唯一码">
					<el-input type="textarea" :autosize="{ minRows: 8, maxRows: 20}" placeholder="已输入的唯一码"
						style="width: 500px" v-model="dataForm.uniquecode">
					</el-input>
				</el-form-item>
			</el-form>
		</div>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
		</template>
	</el-dialog>
	<SkuDialog v-if="skuDialogVisible" ref="skuDialogVue" @fillline="fillLine"></SkuDialog>
</template>

<script>
	import {
		ref,
		reactive,
		onMounted,
		nextTick,
		computed
	} from 'vue';
	import {
		getReceiptdetailById,
		putReceiptdetail,
		postReceiptdetail
	} from "@/network/lswms/receipt/Receiptdetail"
	import {
		ElMessage
	} from 'element-plus'
	import {
		getCodeListByCode
	} from "@/network/lswms/base/Codelist.js"
	import {
		getReceiptdetailCount
	} from "@/network/lswms/receipt/Receiptdetail.js"

	import _ from 'lodash'

	import {
		getSkuById,
		getSkuByCode
	} from "../../../network/lswms/base/Sku";

	import {
		getLotByLotN
	} from "@/network/lswms/stock/Lot.js"
	import {
		request
	} from "../../../network/request";
	import SkuDialog from "@/components/lswms/base/SkuDialog.vue"

	export default {
		name: "receiptdetailAddOrUpdate",
		emits: ["refreshDataList"],
		components: {
			SkuDialog
		} ,
		data() {
			return {

				innerVisible: false,
			};
		},
		setup(props, {
			emit
		}) {
			
			
			const outerVisible = ref(false)
			const visible = ref(false);

			const dataForm = ref({
				id: '',
				receiptkey: '',
				line: '',
				sku: '',
				unit: '',
				pack: '',
				status: 0,
				qty: '0',
				actqty: '0',
				toloc: 'PA',
				totray: '',
				tobox: '',
				lot: '',
				lot1: '',
				lot2: '',
				lot3: '',
				lot4: '',
				lot5: '',
				lot6: '',
				lot7: '',
				lot8: '',
				lot9: '',
				lot10: '',
				lot11: '',
				lot12: '',
				lot13: '',
				lot14: '',
				lot15: '',
				remark: '',
				sourcekey: '',
				sourcetype: '',
				sourcedate: '',
				relatedorderkey: '',
				relatedorderline: '',
				backstatus: '',
				backdate: '',
				backresult: '',
				backcode: '',
				warehouse: '',
				adduser: '',
				addtime: '',
				updatuser: '',
				updatetime: '',
				isuniquecode: '',
				uniquecode: '',
			});

			
			const dataRule = reactive({
				receiptkey: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				sku: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				// unit: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				// pack: [{
				// 	required: true,
				// 	message: '必填项不能为空',
				// 	trigger: 'blur'
				// }],
				status: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],
				qty: [{
					required: true,
					message: '必填项不能为空',
					trigger: 'blur'
				}],

			})
			const dataFormRef = ref(null)
			const init = () => {
				visible.value = true;
				receiptStatusFn();
				boxsFn();
				yesornoFn();
				newandoldsFn();
				nextTick(function() {
					dataFormRef.value.resetFields();
					if (dataForm.value.id) {
						getreceiptdetailInfo();
					} else {

					}
				})
			}
			const getreceiptdetailInfo = () => {
				getReceiptdetailById(dataForm.value.id).then(function(res) {
					if (res.code !== 0) {
						return ElMessage.error(res.msg);
					}
					dataForm.value = _.assignIn({}, dataForm.value, res.data);
				}).catch((err) => {});
			}
			// 获取状态
			const receiptStatus = ref("");
			const receiptStatusFn = () => {
				getCodeListByCode('receiptstatus').then((res) => {
					if (res.code !== 0) {
						return ElMessage.error(res.msg);
					}
					receiptStatus.value = res.data;
				}).catch((err) => {});
			}
			//获取行号
			/*const receiptdetailLineFn =()=>{

			         getReceiptdetailCount(dataForm.value.receiptkey).then((res)=>{
			                if (res.code !== 0) {
			                        return ElMessage.error(res.msg);
			                }
			                dataForm.value.line = res.data+1;
			        })
			}
			*/

			const getSkuFn = () => {
				let sku = dataForm.value.sku;
				if (!sku) {
					return ElMessage.error("请输入物料码");
				}

				getSkuByCode(sku).then((res) => {
					if (res.code !== 0) {

						return ElMessage.error(res.msg);
					}
					dataForm.value.sku = res.data.sku
					dataForm.value.descr = res.data.descr
					dataForm.value.factorycdoe = res.data.factorycdoe
					dataForm.value.pack = res.data.pack
					dataForm.value.sort = res.data.sort
					dataForm.value.unit = res.data.unit
					dataForm.value.weight = res.data.weight
					dataForm.value.length = res.data.length
					dataForm.value.width = res.data.width
					dataForm.value.lotrule = res.data.lotrule
					dataForm.value.isuniquecode = res.data.isuniquecode
				}).catch()
			}

			const boxs = ref("");
			const boxsFn = () => {

				getCodeListByCode('BOXLIST').then((res) => {
					if (res.code !== 0) {
						return ElMessage.error(res.msg);
					}
					boxs.value = res.data;
				}).catch((err) => {});
			}

			const newandolds = ref("");
			const newandoldsFn = () => {
				getCodeListByCode('NEWANDOLD').then((res) => {
					if (res.code !== 0) {
						return ElMessage.error(res.msg);
					}
					newandolds.value = res.data;
				}).catch((err) => {});
			}

			const yesornos = ref("");
			const yesornoFn = () => {
				getCodeListByCode('YESORNO').then((res) => {
					if (res.code !== 0) {
						return ElMessage.error(res.msg);
					}
					yesornos.value = res.data;
				}).catch((err) => {});
			}

			// 表单提交
			const dataFormSubmitHandle = _.debounce(() => {
				dataFormRef.value.validate(function(valid) {
					if (!valid) {
						return false;
					}
					let qing = () => {
						return !dataForm.value.id ? postReceiptdetail(dataForm.value) :
							putReceiptdetail(dataForm.value)
					}
					qing().then(function(res) {
						if (res.code !== 0) {
							return ElMessage.error(res.msg);
						}
						ElMessage({
							message: '操作成功',
							type: 'success',
							duration: 500,
							onClose: function() {
								visible.value = false
								emit('refreshDataList')
							}
						})
					}).catch(function() {});

				})
			}, 1000, {
				'leading': true,
				'trailing': false
			})

			const isuniqueVisible = ref(false)

			const message = ref('')
			const putMessage = () => {
				if (message.value == '') {
					return;
				}
				let uniquecodes = dataForm.value.uniquecode.split(',')
				let newArr = uniquecodes.filter(function(value, index, array) {
					return value == message.value;
				});

				if (newArr.length == 0) {
					if (dataForm.value.uniquecode.length == 0) {
						dataForm.value.uniquecode = message.value
					} else {
						dataForm.value.uniquecode = dataForm.value.uniquecode + '\n' + message.value
					}
				}
				message.value = ''
			}


			const getLot6Fn = () => {
				let lot6 = dataForm.value.lot6;
				if (!lot6) {
					return ElMessage.error("计划单号/工序号");
				}

				getLotByLotN({
					lot6
				}).then((res) => {
					if (res.code != 0) {

						return ElMessage.error(res.msg);
					}
					let lotlists = res.data;
					if (lotlists.length > 0) {

						let lotlist = lotlists[0];
						dataForm.value.sku = lotlist.sku;
						dataForm.value.lot1 = lotlist.lot1;
						dataForm.value.lot2 = lotlist.lot2;
						dataForm.value.lot3 = lotlist.lot3;
						dataForm.value.lot4 = lotlist.lot4;
						dataForm.value.lot5 = lotlist.lot5;

						dataForm.value.lot7 = lotlist.lot7;
						dataForm.value.lot8 = lotlist.lot8;
						dataForm.value.lot9 = lotlist.lot9;
						dataForm.value.lot10 = lotlist.lot10;
						dataForm.value.lot11 = lotlist.lot11;
						dataForm.value.lot12 = lotlist.lot12;
						dataForm.value.lot13 = lotlist.lot13;
						dataForm.value.lot14 = lotlist.lot14;
						dataForm.value.lot15 = lotlist.lot15;

						getSkuFn();
					}

				}).catch()
			}


			const skuDialogVisible = ref(false);
			const skuDialogVue = ref(null);
			const openSkuDialogHandle = () => {
				skuDialogVisible.value = true;
				nextTick(function() {
					//skuDialogVue.value.dataForm.sku = dataForm.value.sku;
					skuDialogVue.value.init();
				})
			}

			const fillLine = (SkuRow) => {
				dataForm.value.sku = SkuRow.sku
				dataForm.value.descr = SkuRow.descr
				dataForm.value.factorycdoe = SkuRow.factorycdoe
				dataForm.value.pack = SkuRow.pack
				dataForm.value.sort = SkuRow.sort
				dataForm.value.unit = SkuRow.unit
				dataForm.value.weight = SkuRow.weight
				dataForm.value.length = SkuRow.length
				dataForm.value.width = SkuRow.width
				dataForm.value.lotrule = SkuRow.lotrule
				dataForm.value.isuniquecode = SkuRow.isuniquecode
			}

			return {

				outerVisible,
				visible,
				dataForm,
				dataRule,
				init,
				dataFormSubmitHandle,
				dataFormRef,
				receiptStatus,

				getSkuFn,
				boxs,
				yesornos,
				isuniqueVisible,
				message,
				putMessage,
				newandolds,
				getLot6Fn,
				skuDialogVisible,
				skuDialogVue,
				openSkuDialogHandle,
				fillLine
			}
		},
		methods: {
			//hasPermission
			reString: function(val) {
				let n = Number(val)
				if (!isNaN(n)) {
					return n
				} else {
					return val
				}
			},

		}

	}
</script>

<style scoped>

</style>
